package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import com.zzyl.vo.UserRoleVo;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 分页查询角色管理
     *
     * @param roleDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto", value = "角色DTO对象", required = true, dataType = "roleDto"),
            @ApiImplicitParam(paramType = "path", name = "pageNum", value = "页码", example = "1", dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "pageSize", value = "每页条数", example = "10", dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"roleDto.roleName"})
    public ResponseResult<PageResponse<RoleVo>> findRoleVoPage(
            @RequestBody RoleDto roleDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {

        PageResponse<RoleVo> list = roleService.list(roleDto, pageNum, pageSize);
        return ResponseResult.success(list);
    }

    /**
     * 角色添加
     */
    @PutMapping
    @ApiOperation("角色添加")
    @ApiImplicitParam(name = "roleDto", value = "角色DTO对象", required = true, dataType = "RoleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName", "roleDto.dataState"})
    public ResponseResult<RoleVo> createRole(@RequestBody RoleDto roleDto) {
        Boolean flag = roleService.add(roleDto);
        return ResponseResult.success(flag);
    }

    /**
     * 根据角色id查询资源列表
     */
    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation("根据角色id查询资源列表")
    public ResponseResult<List<RoleVo>> findCheckedResources(@PathVariable("roleId") Long roleId){
        List<RoleVo> list = roleService.findCheckedResources(roleId);
        return ResponseResult.success(list);
    }
    /**
     * 删除角色
     */
    @DeleteMapping("/{roleIds}")
    public ResponseResult<RoleVo> delete(@PathVariable("roleIds") String roleIds){
        roleService.deleteRoleById(roleIds);
        return ResponseResult.success();
    }
    /**
     * 角色修改
     */
    @PatchMapping
    @ApiOperation(value = "角色修改",notes = "角色修改")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "RoleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState","roleDto.dataScope","roleDto.checkedResourceNos","roleDto.checkedDeptNos","roleDto.id"})
    public ResponseResult<Boolean>update(@RequestBody RoleDto roleDto) throws CloneNotSupportedException {
        Boolean flag = roleService.updateRole(roleDto);
        return ResponseResult.success(flag);
    }

    /**
     * 角色下拉框
     */
    @PostMapping("/init-roles")
    @ApiOperation("角色下拉框")
    public ResponseResult <List<RoleVo>> roleSelect(){
        List<RoleVo> list =  roleService.findAll();
        return ResponseResult.success(list);
    }
}
